package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApFollowBehaviorMapper;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApFollowBehaviorService;
import com.heima.model.behavior.dtos.FollowBehaviorDto;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApFollowBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * APP关注行为表 服务实现类
 * </p>
 *
 * @author itheima
 */
@Slf4j
@Service
@Transactional
public class ApFollowBehaviorServiceImpl extends ServiceImpl<ApFollowBehaviorMapper, ApFollowBehavior> implements ApFollowBehaviorService {

    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;

    @Override
    public ResponseResult saveFollowBehavior(FollowBehaviorDto dto) {
        //根据传来的参数去查询到对应行为实体
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApBehaviorEntry behaviorentry = apBehaviorEntryService.findByUserIdOrEquipmentId(dto.getUserId(), null);
        //如果实体存在 利用他 如果不存在 创建
        //但是老师要求的好像是 如果不存在 直接返回
        if (behaviorentry == null) {
//            behaviorentry.setType((short) 1);
//            behaviorentry.setCreatedTime(new Date());
//            behaviorentry.setEntryId(dto.getUserId());
//            apBehaviorEntryService.save(behaviorentry);
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //保存关注表
            ApFollowBehavior followBehavior = new ApFollowBehavior();
            followBehavior.setArticleId(dto.getArticleId());
            followBehavior.setCreatedTime(new Date());
            followBehavior.setEntryId(behaviorentry.getId());
            followBehavior.setFollowId(dto.getFollowId());
            boolean b = save(followBehavior);
        return ResponseResult.okResult(b);
    }
}